package com.hzlx.dao.impl;

import com.hzlx.dao.BaseDao;
import com.hzlx.dao.TBusinessInfoDao;
import com.hzlx.entity.TBusinessInfo;
import com.hzlx.entity.TOrderInfo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 86166
 * @version 1.0
 * @description: TODO
 * @date 2023/4/9 22:34
 */
public class TBusinessInfoDaoImpl extends BaseDao<TBusinessInfo> implements TBusinessInfoDao{
    @Override
    public int deleteByPrimaryKey(Integer id) {
        String sql = "delete * from t_business_info where id=?";
        return executeUpdate(sql, id);
    }

    @Override
    public int insert(TBusinessInfo record) {
        String sql = "insert into t_business_info values(?,?,?,?,?,?,?,default,default)";
        return executeUpdate(sql,null,record.getName(),record.getUserName(),record.getPassword(),record.getAddress(),record.getTel(),new Date());
    }

    @Override
    public int insertSelective(TBusinessInfo record) {
        return 0;
    }

    @Override
    public TBusinessInfo selectByPrimaryKey(Integer id) {
        String sql = "select * from t_business_info where id = ?";
        List<TBusinessInfo> tBusinessInfos = executeQuery(sql, TBusinessInfo.class, id);
        TBusinessInfo tBusinessInfo = null;
        for (TBusinessInfo businessInfo : tBusinessInfos) {
            tBusinessInfo = businessInfo;
        }
        return tBusinessInfo;
    }

    @Override
    public TBusinessInfo selectByUserNameAndPassword(String userName,String password) {
        String sql = "select * from t_business_info where user_name=? and password=?";
        List<TBusinessInfo> tBusinessInfos = executeQuery(sql, TBusinessInfo.class, userName, password);
        TBusinessInfo tBusinessInfo = null;
        if (tBusinessInfos.size()>0){
            for (TBusinessInfo businessInfo : tBusinessInfos) {
                tBusinessInfo = businessInfo;
            }
        }

        return tBusinessInfo;
    }

    @Override
    public int updateByPrimaryKeySelective(TBusinessInfo record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(TBusinessInfo record) {
        String sql = "update t_business_info set name=?, user_name=?, password= ?,address=?,tel=?,job_type=?,status=? where id=?";
        return executeUpdate(sql,record.getName(),record.getUserName(),record.getPassword(),record.getAddress(),record.getTel(),record.getJobType(),record.getStatus(),record.getId());
    }

    @Override
    public BigDecimal selectProjectedIncome(Integer businessId) {
        String sql = "select sum(total_money) as projected_income from t_order_info where business_id=?";
        Map<String, Object> map = executeQueryMap(sql, businessId);
        return (BigDecimal) map.get("projected_income");
    }

    @Override
    public Long selectPendingOrderNo(Integer id) {
        String sql = "select count(1) as count from t_order_info where business_id=?";
        Map<String, Object> map = executeQueryMap(sql, id);
        return (Long)map.get("count");
    }

    @Override
    public List<TBusinessInfo> selectBusinessAll() {
        String sql = "select * from t_business_info";
        return executeQuery(sql, TBusinessInfo.class);
    }


}
